2-sat brute force constructive algorithms greedy *1500

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    s = input()
    x = int(input())
    ls = len(s)
    a = [1] * ls
    for i, k in enumerate(s):
        if k == '0':
            if i-x>=0: a[i-x] = 0
            if i + x < ls: a[i+x] = 0
    for i, k in enumerate(s):
        if k == '1':
            f = 0
            if i-x>=0: f |= a[i-x]
            if i + x < ls: f |= a[i+x]
            if not f:
                print(-1)
                break
    else: print(''.join(str(a) for a in a))
 	 		    							 							 	

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define ull unsigned long long 
#define lld long double
#define lcm(x,y) (x/__gcd(x,y)*y)
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define setbit __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fr(i ,t , n) for (int i = t; i <= n; i++)
#define ceilval(a,b) ((a / b) + ((a % b) != 0));
#define endl "\n"
#define ub upper_bound
#define lb lower_bound
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

const ll M=1e9+7;
ll binexp(ll a,ll b,ll m){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(1LL*ans*a)%m;
        }
        a=(1LL*a*a)%m;
        b=(b>>1);
    }
    return ans;
}

bool ispal(string S){
    for (int i = 0; i < S.length() / 2; i++) {
        if (S[i] != S[S.length() - i - 1]) return 0;
    }
    return 1;
}

bool isPrime(ll n){
    if (n <= 1) return 0;
    if (n == 2 || n == 3) return 1;
    if (n % 2 == 0 || n % 3 == 0) return 0;
    for (ll i = 5; i <= sqrt(n); i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return 0;
     return 1;
}

vector<int> pfsq(int n){
    vector<int>ans;
    for(int i=2 ; i*i<=n; ++i){
        while(n%i==0){
            ans.pb(i);
            n/=i;
        }
    }
    if(n>1) ans.pb(n);
    return ans;
}

//---------------------------------------------------------------------

// const int NN=1e7+10;
// ll fact[NN];

// void initfact(){
//     fact[0]=1;
//     for (int i = 1; i < NN; ++i){
//         fact[i]=(i*fact[i-1]*1LL)%M;
//     }
// }

// ll nck(ll n,ll k){
//     ll ans=fact[n];
//     ll deno=(fact[k]*fact[n-k]*1LL)%M;
//     ans=(ans*binexp(deno,M-2,M)*1LL)%M;
//     return ans;
// }

//-------------------

// const int NN=1e7+10;
// vector<int>b(NN,1);
// // vector<int>hp(NN,0);

// void sieve(){
//     b[0]=b[1]=0;
//     for(int i=2; i<NN; i++){
//         if(b[i]){
//             // hp[i]=i;
//             for(int j=2*i; j<NN; j+=i){
//                 b[j]=0;
//                // hp[j]=i;
//             }
//         }
//     }
// }

// vector<int> pfsieve(int n){
//     vector<int>ans;
//     while(n>1){
//         int p=hp[n];
//         while(n%p==0){
//             n=n/p;
//             ans.pb(p);
//         }
//     }
//     return ans;
// }

//---------------------------------------------------------------------



void solve(){
    ll tt=1; 
    cin>>tt; 
    while(tt--){
        string s; cin>>s;
        ll n=sz(s);
        ll x; cin>>x;
        string tmp(n,'1');
        for(int i=0; i<n; i++){
            if(s[i]=='0'){
                if(i-x>=0) tmp[i-x]='0';
                if(i+x<n) tmp[i+x]='0';
            }
        }
        string a(n,'1');
        for(int i=0; i<n; i++){
            if(((i-x<0) || (i-x>=0 && tmp[i-x]=='0')) && ((i+x>=n) || (i+x<n && tmp[i+x]=='0'))) a[i]='0';
        }
        // cout<<tmp<<endl;
        // cout<<a<<endl;
        if(a==s) cout<<tmp<<endl;
        else cout<<-1<<endl;
    }
}


//---------------------------------------------------------------------
int main() {
    #ifndef ONLINE_JUDGE
      freopen("dbg.txt","w",stderr);
    #endif
    fastio();

    solve();
}


Comments

Submit
0 Comments
More Questions

1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K